﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yule8xCommon.Applib;
using Yule8xCommon.Typelib;
using Yule8xModel;
using Yule8xWechat.Applib;

namespace Yule8xWechat.Dimond
{
    public partial class DimondTransfer : System.Web.UI.Page
    {
        protected int dimond_blance, user_id;
        protected WxUserInfoResult wxUser = null;
        protected int game_id_bozhong, game_id_wanghu, bozhongrate, wanghurate;
        protected void Page_Load(object sender, EventArgs e)
        {
            AppInfo.IsDebugMode = RuntimeDataUtil.CheckDebugModel();

            user_id = RequestUtil.GetInteger("user_id");
            if (AppInfo.IsDebugMode && user_id > 0)
            {
                wxUser = UserModels.GetWxUserInfo(user_id);
                if (null == wxUser)
                {
                    ResponseUtil.WriteEnd("user_id无效!");
                }
            }
            else
            {
                wxUser = LoginPageBase.GetLoginInfo();
                if (null == wxUser)
                {
                    Response.Redirect(WechatUrls.GetUserInfoUrl("diamondtransfer"), true);
                }
            }
            /*
            // 判断登录
            object loginData = Session["login"];
            if (null != loginData)
            {
                wxUser = loginData as WxUserInfoResult;
            }
            if (null == loginData || null == wxUser)
            {
                LogUtil.Instance.WriteInfo("[钻石转入游戏] 未登录");
                Response.Redirect(WechatUrls.GetUserInfoUrl("promotion"), true);
            }
            */
            bozhongrate = AppConf.GameCurrencyRate_Bozhong;
            wanghurate = AppConf.GameCurrencyRate_Wanghu;

            int uid = wxUser.user_id;
            LogUtil.Instance.WriteInfo("[钻石转入游戏] 已登录");
            dimond_blance = UserModels.GetDimondBlance(wxUser.user_id);

            int query_game_id = RequestUtil.GetInteger("game_id");
            int query_game_mact_id = RequestUtil.GetInteger("game_mact_id");
            switch (query_game_mact_id)
            {
                case 1:
                    game_id_bozhong = query_game_id > 0 ? query_game_id : wxUser.game_id_bozhong;
                    break;
                case 2:
                    game_id_wanghu = query_game_id > 0 ? query_game_id : wxUser.game_id_wanghu;
                    break;
            }

            string action = RequestUtil.FormString("action", maxLen: 16);
            if ("submit" == action)
            {
                int game_id = RequestUtil.FormInteger("game_id");
                int number = RequestUtil.FormInteger("number");
                int game_mact_id = RequestUtil.FormInteger("game_mact_id");
                int game_currency_number = number;
                switch (game_mact_id)
                {
                    case 1:
                        game_currency_number = number * AppConf.GameCurrencyRate_Bozhong;
                        break;
                    case 2:
                        game_currency_number = number * AppConf.GameCurrencyRate_Wanghu;
                        break;
                }
                string msg;
                bool result = GiveCardModels.ConvertDimondToGameCurrency(uid, game_id, number, game_currency_number, game_mact_id, out msg);
                // 日志
                LogUtil.Instance.WriteInfo(string.Format("[钻石转入游戏] DimondTransfer.aspx: GiveCardModels.ConvertDimondToGameCurrency({0}, {1}, {2}, {3}, {4}); result={5},message={6}", uid, game_id, number, game_currency_number, game_mact_id, result, msg));

                Response.Redirect("ResultJump.aspx?result=" + msg);
            }
        }
    }
}